//
//  UIViewController+SEGBaseMethod.m
//  SEGWorkOrderTest
//
//  Created by Clavis on 2019/4/4.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "UIViewController+SEGBaseMethod.h"
#import <objc/runtime.h>

#if __has_include(<Commom/SEGSwizzle.h>)
#import <Commom/SEGSwizzle.h>
#else
#import "SEGSwizzle.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

@interface UIViewController ()

@property (nonatomic, assign) CGRect tempFrame; //适配开启热点的时候 保存之前的frame
@property (nonatomic, assign) BOOL isAlreadyOpenHotspot; //已经开启了热点

@end

@implementation UIViewController (SEGBaseMethod)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEGSwizzleMethod([self class], @selector(viewWillLayoutSubviews), [self class], @selector(segBaseMethd_viewWillLayoutSubviews));
    });
}

#pragma mark - 键盘相关
/**
 *  @author 肖湘军, 16-12-31 15:12:36
 *
 *  弹出键盘提示完成按钮
 *
 *  @return 返回底视图
 */
- (UIView*)onKeyBoardFinishedButton
{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    UIView* tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, 40)];
    tempView.backgroundColor = kColor_bg;
    
    //加条线
    UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, CGRectGetWidth(tempView.bounds), 0.5)];
    lineView.backgroundColor = kColor_L2;
    [tempView addSubview:lineView];
    
    UIButton* finishBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(tempView.bounds) - 60, 5, 60, 30)];
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(onKeyBoardFinishedButton:) forControlEvents:UIControlEventTouchUpInside];
    [tempView addSubview:finishBtn];
    [finishBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [finishBtn setTitleColor:kColor_1 forState:UIControlStateNormal];
    
    return tempView;
}

/**
 点击了键盘的完成按钮【需要子类重写】
 
 @param sender 按钮
 */
- (void)onKeyBoardFinishedButton:(UIButton*)sender {
}

#pragma mark - 适配热点
- (void)segBaseMethd_viewWillLayoutSubviews {
    [self segBaseMethd_viewWillLayoutSubviews];
    
    if (self.isNeedAdpterHotspot) {
        CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
        if (statusBarRect.size.height == 40) {
            if (self.tempFrame.size.width != self.view.frame.size.width || self.tempFrame.size.height != self.view.frame.size.height) {
                self.tempFrame = self.view.frame;
            }
            self.isAlreadyOpenHotspot = YES;
            self.view.frame = CGRectMake(self.tempFrame.origin.x, self.tempFrame.origin.y - 20, self.tempFrame.size.height, self.tempFrame.size.width);
        } else {
            if (self.isAlreadyOpenHotspot) {
                self.isAlreadyOpenHotspot = NO;
                self.view.frame = self.tempFrame;
            }
            self.tempFrame = self.view.frame;
        }
    }
}

#pragma mark - Associate object
- (void)setTempBgView:(UIView *)tempBgView {
    objc_setAssociatedObject(self, @selector(tempBgView), tempBgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)tempBgView {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setTempData:(id)tempData {
    objc_setAssociatedObject(self, @selector(tempData), tempData, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)tempData {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setIsNeedAdpterHotspot:(BOOL)isNeedAdpterHotspot {
    objc_setAssociatedObject(self, @selector(isNeedAdpterHotspot), @(isNeedAdpterHotspot), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isNeedAdpterHotspot {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setIsAlreadyOpenHotspot:(BOOL)isAlreadyOpenHotspot {
    objc_setAssociatedObject(self, @selector(isAlreadyOpenHotspot), @(isAlreadyOpenHotspot), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isAlreadyOpenHotspot {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setTempFrame:(CGRect)tempFrame {
    objc_setAssociatedObject(self, @selector(tempFrame), [NSValue valueWithCGRect:tempFrame], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGRect)tempFrame {
    return [objc_getAssociatedObject(self, _cmd) CGRectValue];
}

@end
